<?php

/*
  Plugin Name: Daily Horoscopes Widget
  Plugin URI: http://dianagarland.com/wp-daily-horoscopes-widget/
  Description: Sidebar widget which displays daily horoscopes from DianaGarland.com.
  Author: Duncan Marshall
  Version: 1.0.0
  Author URI: http://duncanmarshall.net/

 * *************************************************************************

  Copyright (C) 2010-2012 Duncan Marshall

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * *************************************************************************

 */

class Daily_Horoscopes_Widget extends WP_Widget {

	public function __construct() {
		parent::__construct(
	 		'Daily_Horoscopes_Widget',
			'Daily Horoscopes',
			array( 'description' => __( 'Displays daily horoscopes from DianaGarland.com.', 'text_domain' ),)
		);
	}

	public function widget( $args, $instance ) {
	
		extract( $args );
		$title = apply_filters( 'widget_title', $instance['title'] );

		echo $before_widget;
		if ( ! empty( $title ) )
			echo $before_title . $title . $after_title;
		
		$this->DoWebEmbedWidget(array(	"fsize" => $instance['fsize'], "fname" => $instance['fname'], 
								"fcolor" => $instance['fcolor'], "wheight" => $instance['widgetheight'],
								"ns" => $instance['ns'], "bcolor" => $instance['bcolor'], "hcolor" => $instance['hcolor'],
								"justlinks" => $instance['justlinks'], "links_enabled" => $instance['links_enabled']));

		echo $after_widget;
	}

	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['fsize'] = strip_tags($new_instance['fsize']);
		$instance['fname'] = strip_tags($new_instance['fname']);
		$instance['fcolor'] = strip_tags($new_instance['fcolor']);
		$instance['bcolor'] = strip_tags($new_instance['bcolor']);
		$instance['hcolor'] = strip_tags($new_instance['hcolor']);
		$instance['ns'] = strip_tags($new_instance['ns']);
		$instance['links_enabled'] = $new_instance['links_enabled'];
		$instance['justlinks'] = $new_instance['justlinks'];
		$instance['widgetheight'] = $new_instance['widgetheight'];

		return $instance;
	}

	public function form( $instance ) {
	
		$showicons = esc_attr( $instance[ 'showicons' ] );
		$links_enabled = esc_attr( $instance[ 'links_enabled' ] );
		$justlinks = esc_attr( $instance[ 'justlinks' ] );
		$title = esc_attr( $instance[ 'title' ] );
		if ($title == ""){$title = "Daily Horoscopes:";}			
		$fsize = esc_attr( $instance[ 'fsize' ] );
		if ($fsize == ""){$fsize = "12";}
		$fname = esc_attr( $instance[ 'fname' ] );
		if ($fname == ""){$fname = "Helvetica";}
		$fcolor = esc_attr( $instance[ 'fcolor' ] );
		if ($fcolor == ""){$fcolor = "909090";}
		$bcolor = esc_attr( $instance[ 'bcolor' ] );
		if ($bcolor == ""){$bcolor = "FFFFFF";}
		$hcolor = esc_attr( $instance[ 'hcolor' ] );
		if ($hcolor == ""){$hcolor = "000000";}
		$ns = esc_attr( $instance[ 'ns' ] );
		if ($ns == ""){$ns = "DHW_";}	
		$wheight = esc_attr( $instance[ 'widgetheight' ] );
		if ($wheight == ""){$wheight = "255";}			
		
		if ($links_enabled != "on"){ ?>
		
			<span style="color:red;">Outside links need to be enabled for the plugin to work properly.  If they're not, the widget renders nothing.</span>
		
		<?php } ?>
		
	
		
		<p>
		<label for="<?php echo $this->get_field_id('links_enabled'); ?>"><?php _e('Allow outside links to horoscopes:'); ?></label> 
		<input id="<?php echo $this->get_field_id('links_enabled'); ?>" name="<?php echo $this->get_field_name('links_enabled'); ?>" type="checkbox" value="on" <?php if ($links_enabled=="on") {echo ' checked="yes"'; } ?>>
		</input>
		</p>
	
		<p>
		<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?></label>
		[<a style="cursor:pointer" title="Will be displayed as the widget title.">?</a>]:
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('fsize'); ?>"><?php _e('Font size (in px):'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('fsize'); ?>" name="<?php echo $this->get_field_name('fsize'); ?>" type="text" value="<?php echo $fsize; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('fname'); ?>"><?php _e("Font name (fills CSS font-family property):"); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('fname'); ?>" name="<?php echo $this->get_field_name('fname'); ?>" type="text" value="<?php echo $fname; ?>" />
		</p>
		
	
		<p>
		<label for="<?php echo $this->get_field_id('widgetheight'); ?>"><?php _e('Widget height (px):'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('widgetheight'); ?>" name="<?php echo $this->get_field_name('widgetheight'); ?>" type="text" value="<?php echo $wheight; ?>" />
		</p>			
		
		<p style="color:#A0A0A0">Colors are <a href="http://www.quackit.com/css/css_color_codes.cfm">RGB hex codes</a>.  Don't include the #.</p>
		
		<p>
		<label for="<?php echo $this->get_field_id('fcolor'); ?>"><?php _e('Title Color:'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('fcolor'); ?>" name="<?php echo $this->get_field_name('fcolor'); ?>" type="text" value="<?php echo $fcolor; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('bcolor'); ?>"><?php _e('Text Color:'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('bcolor'); ?>" name="<?php echo $this->get_field_name('bcolor'); ?>" type="text" value="<?php echo $bcolor; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('hcolor'); ?>"><?php _e('Background Color:'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('hcolor'); ?>" name="<?php echo $this->get_field_name('hcolor'); ?>" type="text" value="<?php echo $hcolor; ?>" />
		</p>
		
		<hr/>
		
		<p>
		<label for="<?php echo $this->get_field_id('justlinks'); ?>"><?php _e('Links only'); ?></label>
		[<a style="cursor:pointer" title="If this isn't checked, clicking on a sign displays a daily horoscope prediction inside your sidebar.  If it is checked, sign names are links pointing to horoscopes at dianagarland.com.  This is good if your users have NoScript.">?</a>]:
		<input id="<?php echo $this->get_field_id('justlinks'); ?>" name="<?php echo $this->get_field_name('justlinks'); ?>" type="checkbox" value="on" <?php if ($justlinks=="on") {echo ' checked="yes"'; } ?>>
		</input>
		</p>			

		<p>
		<label for="<?php echo $this->get_field_id('ns'); ?>"><?php _e('Namespace'); ?></label> 
		[<a style="cursor:pointer" title="The scripts and styles for this widget are namespaced to avoid conflicts.  You're probably fine with the default setting.">?</a>]:
		<input class="widefat" id="<?php echo $this->get_field_id('ns'); ?>" name="<?php echo $this->get_field_name('ns'); ?>" type="text" value="<?php echo $ns; ?>" />
		</p>	

		<?php 
	}
	
	private function DoWebEmbedWidget($Args) {

		if ($Args['links_enabled'] != "on"){return;}

		$FontSize = 10; //Base Font size.  Other font sizes are derived from this.
		$FontName = 'Helvetica';//Font to be used for the widget
		$FontColor = 'black';//Color of the text in the widgets.  This includes links.
		$JustLinks = '';//Decides whether dyniamic content should be used.
		$NS = 'DHW_';//sets a unique namespace prefix.
		$BackColor = 'fff';//Background color of widget
			
		$JustLinks = $Args['justlinks'];

		if (is_numeric($Args['fsize'])){
			
			$FontSize = $Args['fsize'];
			
		}

		if ($Args['fname'] != null){$FontName = $Args['fname'];}
		if ($Args['ns'] != null){$NS = $Args['ns'];}
		if ($Args['bcolor'] != null){$BackColor = $Args['bcolor'];}
		if ($Args['hcolor'] != null){$HighlightColor = $Args['hcolor'];}
		if ($Args['fcolor'] != null){$FontColor = $Args['fcolor'];}
		if ($Args['wheight'] != null){$WidgetHeight = $Args['wheight'];}
		
	?>


		<div id="<?php echo $NS ?>Controls">

			<ul>

				<li id="<?php echo $NS."Aries"; ?>">

					<a target="_blank" href="http://dianagarland.com/aries-horoscope/">Aries</a>
				
				</li>
				
				<li id="<?php echo $NS."Taurus"; ?>">

					<a target="_blank" href="http://dianagarland.com/taurus-horoscope/">Taurus</a>
				
				</li>

				<li id="<?php echo $NS."Gemini"; ?>">

					<a target="_blank" href="http://dianagarland.com/gemini-horoscope/">Gemini</a>
				
				</li>
				
				<li id="<?php echo $NS."Cancer"; ?>">

					<a target="_blank" href="http://dianagarland.com/cancer-horoscope/">Cancer</a>
				
				</li>

				<li id="<?php echo $NS."Leo"; ?>">

					<a target="_blank" href="http://dianagarland.com/leo-horoscope/">Leo</a>
				
				</li>
				
				<li id="<?php echo $NS."Virgo"; ?>">

					<a target="_blank" href="http://dianagarland.com/virgo-horoscope/">Virgo</a>
				
				</li>

				<li id="<?php echo $NS."Libra"; ?>">

					<a target="_blank" href="http://dianagarland.com/libra-horoscope/">Libra</a>
				
				</li>
				
				<li id="<?php echo $NS."Scorpio"; ?>">

					<a target="_blank" href="http://dianagarland.com/scorpio-horoscope/">Scorpio</a>
				
				</li>

				<li id="<?php echo $NS."Sagittarius"; ?>">

					<a target="_blank" href="http://dianagarland.com/sagittarius-horoscope/">Sagittarius</a>
				
				</li>
				
				<li id="<?php echo $NS."Capricorn"; ?>">

					<a target="_blank" href="http://dianagarland.com/capricorn-horoscope/">Capricorn</a>
				
				</li>		

				<li id="<?php echo $NS."Aquarius"; ?>">

					<a target="_blank" href="http://dianagarland.com/aquarius-horoscope/">Aquarius</a>
				
				</li>
				
				<li id="<?php echo $NS."Pisces"; ?>">

					<a target="_blank" href="http://dianagarland.com/pisces-horoscope/">Pisces</a>
				
				</li>	
			
			</ul>

		</div>

		<?php if ($JustLinks != 'on'){?>

			<div id="<?php echo $NS ?>ScopeBox" style="display:none;">

				<p><a href="javascript:<?php echo $NS ?>.SwitchToControls();"><< Other Signs</a></p> 

				<iframe  id="<?php echo $NS; ?>Iframe" width="100%" src="" style="min-height:<?php echo $WidgetHeight; ?>px;"></iframe>

			</div>

			<script type="text/javascript" language="javascript">

				var <?php echo $NS ?> = new function <?php echo $NS ?>Dec(){
					
					var Controls;
					var ScopeBox;
					var ContentFrame; 
					
					this.StartUp = function StartUp(){
					
						this.Controls = document.getElementById('<?php echo $NS; ?>Controls');
						this.ScopeBox = document.getElementById('<?php echo $NS; ?>ScopeBox');
						this.ContentFrame = document.getElementById('<?php echo $NS; ?>Iframe');
						

						
						var This = this;
						
						document.getElementById("<?php echo $NS; ?>" + "Aries").children[0].onclick = function (){

							This.SwitchToSign("Aries");
							return false;

						}
						
						document.getElementById("<?php echo $NS; ?>" + "Taurus").children[0].onclick = function (){

							This.SwitchToSign("Taurus");
							return false;

						}
						
						document.getElementById("<?php echo $NS; ?>" + "Gemini").children[0].onclick = function (){

							This.SwitchToSign("Gemini");
							return false;

						}

						
						document.getElementById("<?php echo $NS; ?>" + "Cancer").children[0].onclick = function (){

							This.SwitchToSign("Cancer");
							return false;

						}
						
						document.getElementById("<?php echo $NS; ?>" + "Leo").children[0].onclick = function (){

							This.SwitchToSign("Leo");
							return false;

						}

						document.getElementById("<?php echo $NS; ?>" + "Virgo").children[0].onclick = function (){

							This.SwitchToSign("Virgo");
							return false;

						}
						
						document.getElementById("<?php echo $NS; ?>" + "Libra").children[0].onclick = function (){

							This.SwitchToSign("Libra");
							return false;

						}

						document.getElementById("<?php echo $NS; ?>" + "Scorpio").children[0].onclick = function (){

							This.SwitchToSign("Scorpio");
							return false;

						}
						
						document.getElementById("<?php echo $NS; ?>" + "Sagittarius").children[0].onclick = function (){

							This.SwitchToSign("Sagittarius");
							return false;

						}

						document.getElementById("<?php echo $NS; ?>" + "Capricorn").children[0].onclick = function (){

							This.SwitchToSign("Capricorn");
							return false;

						}
						
						document.getElementById("<?php echo $NS; ?>" + "Aquarius").children[0].onclick = function (){

							This.SwitchToSign("Aquarius");
							return false;

						}					
						
						document.getElementById("<?php echo $NS; ?>" + "Pisces").children[0].onclick = function (){

							This.SwitchToSign("Pisces");
							return false;

						}	
					
					}
					
					this.SwitchToSign = function SwitchToSign(SignName){

						this.Controls.style.display = 'none';
						this.ScopeBox.style.display = '';
						
						var FrameSrc = "http://dianagarland.com/wdgt/?sign=" + SignName + "&frmt=" + "<?php echo $Format; ?>";
						FrameSrc = FrameSrc + "&fsize=" + "<?php echo $FontSize; ?>";
						FrameSrc = FrameSrc + "&fname=" + "<?php echo $FontName; ?>" + "&fcolor=" + "<?php echo $FontColor; ?>";
						FrameSrc = FrameSrc + "&hcolor=" + "<?php echo $HighlightColor; ?>" + "&bcolor=" + "<?php echo $BackColor; ?>";
						FrameSrc = FrameSrc + "&source=wp-widget";

						this.ContentFrame.src = FrameSrc;
						
					}
					
					this.SwitchToControls = function SwitchToControls(){

						this.ScopeBox.style.display = 'none';
						this.Controls.style.display = '';
						
					}

				}

				<?php echo $NS ?>.StartUp();

			</script> 

			<?php

		}

	}


} // class Daily_Horoscope_Widget


add_action( 'widgets_init', create_function( '', 'register_widget("Daily_Horoscopes_Widget");' ) );



?>